package com.normal.main.fragment

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.normal.main.R
import com.normal.main.databinding.FragmentMainBinding
import com.normal.zbase.impl.base.BaseFragment
import com.normal.zbase.impl.base.BaseViewPager2Adapter
import com.normal.zbase.impl.tools.extend.logger
import com.normal.zbase.impl.tools.extend.viewBinding
import com.normal.zbase.impl.tools.obj.NavigationBarViewObj.initNavigationBar


/**
 * Created by zsf on 2023/3/28 14:52
 * *******************
 *
 * *******************
 */
class MainFragment : BaseFragment<FragmentMainBinding>() {
    private var fragmentList = mutableListOf<Fragment>()
    override fun viewBinding(): FragmentMainBinding {
        return FragmentMainBinding.inflate(layoutInflater)
    }

    override fun initView(view: View, savedInstanceState: Bundle?) {
        super.initView(view, savedInstanceState)
        val userId = arguments?.getString("userId")
        logger.info(userId?:"123")
        initView()
        initListener()
    }

    private fun initView() {
        fragmentList.add(HomeFragment())
        fragmentList.add(OrderFragment())
        fragmentList.add(MineFragment())

        val viewPager2Adapter = BaseViewPager2Adapter(requireActivity(), fragmentList)
        viewBinding.vpMain.adapter = viewPager2Adapter

    }


    /**
     * @see initView
     */
    private fun initListener() {
        with(viewBinding) {
            initNavigationBar(navView,requireContext())
            navView.setOnItemSelectedListener { item ->
                when (item.itemId) {
                    R.id.nav_home -> {
                        vpMain.currentItem = 0
                    }
                    R.id.nav_order -> {
                        vpMain.currentItem = 1
                    }
                    R.id.nav_mine -> {
                        vpMain.currentItem = 2
                    }
                }
                true
            }

            vpMain.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                override fun onPageSelected(position: Int) {
                    super.onPageSelected(position)
                    when (position) {
                        0 -> {
                            navView.selectedItemId = R.id.nav_home
                        }
                        1 -> {
                            navView.selectedItemId = R.id.nav_order
                        }
                        2 -> {
                            navView.selectedItemId = R.id.nav_mine
                        }
                    }
                }
            })
        }


    }

}